package it.zqdszb.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

import butterknife.BindView;
import it.zqdszb.R;
import it.zqdszb.entity.TabEntity;
import it.zqdszb.library.adapter.pagerAdapter.BottomNavigationAdapter;
import it.zqdszb.library.ui.BaseMVPActivity;
import it.zqdszb.ui.fragment.RedPacketFragment;
import it.zqdszb.ui.fragment.TvFragment;

public class MainActivity extends BaseMVPActivity {
	private static final String TAG = "MainActivity";
	@BindView(R.id.parentViewPager)
	ViewPager parentViewPager;


	@BindView(R.id.ll_bottom)
	CommonTabLayout ll_bottom;

	private ArrayList<Fragment> mFragmentList;
	private String[] mTitles = {"电视", "红包", "我的"};
	private int[] mIconSelectIds = {
			R.mipmap.tab_tv_select, R.mipmap.tab_red_packet_select,
			R.mipmap.tab_person_select};
	private int[] mIconUnselectIds = {
			R.mipmap.tab_tv_unselect, R.mipmap.tab_red_packet_unselect,
			R.mipmap.tab_person_unselect};
	private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		mFragmentList = new ArrayList<Fragment>();
		mFragmentList.add(new TvFragment());
		mFragmentList.add(new RedPacketFragment());
		mFragmentList.add(new RedPacketFragment());
		for (int i = 0; i < mTitles.length; i++) {
			mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
		}
		ll_bottom.setTabData(mTabEntities);
		ll_bottom.setOnTabSelectListener(new OnTabSelectListener() {
			@Override
			public void onTabSelect(int position) {
				parentViewPager.setCurrentItem(position);
			}

			@Override
			public void onTabReselect(int position) {
			}
		});

		parentViewPager.setOffscreenPageLimit(0);
		parentViewPager.setAdapter(new BottomNavigationAdapter(getSupportFragmentManager(), mFragmentList));
		parentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}
			@Override
			public void onPageSelected(int position) {
				ll_bottom.setCurrentTab(position);
			}
			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});
		parentViewPager.setCurrentItem(0);
	}
}
